package com.apobates.forum.toro.proxy;

import java.util.function.Function;
/**
 * 查询表达式
 * 适用于: 一对一 , 多对一
 * @param <R> 查询结果类型
 *
 * @author xiaofanku@live.cn
 * @since 20220517
 */
public class MapperAttrExpression<R> {
    private final String targetAttr;
    private final String collectionType;
    private final String selectAttr;
    private final String selectAttrTypeName;
    private final Function<String,R> selectFun;

    /**
     * MapperAttrExpression构造函数
     * @param targetAttr 目标对象的属性名称, 例: Vo.product
     * @param collectionType 目标属性集合的实例供应函数
     * @param selectAttr 查询参数的属性名称, 例: Vo.productId
     * @param selectAttrTypeName 查询参数的类型名称, 例: 基础类型: long, 类类型: java.lang.Long
     * @param selectFun  目标对象属性值的查询函数
     */
    public MapperAttrExpression(
            String targetAttr,
            String collectionType,
            String selectAttr,
            String selectAttrTypeName,
            Function<String,R> selectFun) {
        this.targetAttr = targetAttr;
        this.collectionType = collectionType;
        this.selectAttr = selectAttr;
        this.selectAttrTypeName = selectAttrTypeName;
        this.selectFun = selectFun;
    }

    /**
     * 返回目标对象的属性名称
     * @return
     */
    public String getTargetAttr() {
        return targetAttr;
    }

    /**
     * 返回查询参数的属性名称
     * @return
     */
    public String getSelectAttr() {
        return selectAttr;
    }

    public String getSelectAttrTypeName() {
        return selectAttrTypeName;
    }

    public String getCollectionType() {
        return collectionType;
    }

    /**
     * 返回目标对象属性值的查询函数
     * @return
     */
    public Function<String, R> getSelectFun() {
        return selectFun;
    }

    /**
     * 返回聚合属性语句构建器
     * @return
     */
    public MapperAttrStatements.Argument buildRelationArgs(){
        return new MapperAttrStatements()
                .new Argument(this.getTargetAttr())
                .collection(this.getCollectionType())
                .used(this.getSelectAttr())
                .usedType(this.getSelectAttrTypeName());
    }
}
